package com.servlet.article;

import com.entity.Code;
import com.service.ArticleService;
import com.utils.JsonUtil;
import com.utils.TimeString;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;

import java.io.IOException;

/**
 * 修改文章信息，根据提交的表单信息来修改文章信息
 * 必选参数：id、title、context
 */
@WebServlet(name = "updateArticle", value = "/updateArticle")
public class UpdateArticle extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doPost(request,response);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        JsonUtil jsonUtil = new JsonUtil();
        String title =request.getParameter("title");
        String context= request.getParameter("context");
        int id= Integer.parseInt(request.getParameter("id"));
        String time= new TimeString().TimeString();
        try {
            ArticleService articleService = new ArticleService();
            int i = articleService.updateArticleById(title,context,time,id);
            Code code = new Code();
            code.setCode("200");
            code.setMsg("修改成功");
            String s = jsonUtil.JavaToJson(code);
            response.getWriter().print(s);
        }catch (Exception e){
            System.out.println("文章更新出错："+e);
            Code code = new Code();
            code.setCode("402");
            code.setMsg("文章修改失败。请稍后再试");

            String s = jsonUtil.JavaToJson(code);
            response.getWriter().print(s);
        }


    }
}
